Trò chơi đua xe động vật trong UNITY Engine
114.952 lượt xem;
- InputProcessor.cs
- InputController /
- Bapk /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class InputProcessor : MonoBehaviour {
5 private Rect rect;
6 private Vector2 position;
7 private bool isTouchedDown;
8 private float width;
9 private float height;
10
11 public void Start()
12 {
13 position = new Vector2((transform.position.x + 4) * 100, (transform.position.y + 2.4f) * 100);
14 width = gameObject.GetComponent<SpriteRenderer>().sprite.rect.width;
15 height = gameObject.GetComponent<SpriteRenderer>().sprite.rect.height;
16 rect = new Rect(position.x - width / 2, position.y - height / 2, width, height);
17 }
18
19 private void calculateTransform()
20 {
21 position = new Vector2((transform.position.x + 4) * 100, (transform.position.y + 2.4f) * 100);
22 //width = gameObject.GetComponent<SpriteRenderer>().sprite.rect.width;
23 //height = gameObject.GetComponent<SpriteRenderer>().sprite.rect.height;
24 rect = new Rect(position.x - width / 2, position.y - height / 2, width, height);
25 }
26
27 public void Update()
28 {
29 if (!InputController.Enabled) return;
30 calculateTransform();
31 if (Input.GetButtonDown("Fire1"))
32 {
33 if (checkBounds(rect, convertTo800x480(Input.mousePosition)))
34 {
35 isTouchedDown = true;
36 if(gameObject.GetComponent<InputAdapter>() != null)
37 gameObject.GetComponent<InputAdapter>().OnTouchDown();
38 InputController.IsScreen = false;
39 }
40 }
41 else if (Input.GetButtonUp("Fire1"))
42 {
43 if (gameObject.GetComponent<InputAdapter>() != null)
44 gameObject.GetComponent<InputAdapter>().OnCheckUp();
45 Vector2 mouse = convertTo800x480(Input.mousePosition);
46 if (isTouchedDown && checkBounds(rect, convertTo800x480(Input.mousePosition)))
47 {
48 if(gameObject.GetComponent<InputAdapter>() != null)
49 gameObject.GetComponent<InputAdapter>().OnTouchUp();
50 }
51 isTouchedDown = false;
52 }
53 }
54
55 private bool checkBounds(Rect rect, Vector2 mouse)
56 {
57 if (rect.x <= mouse.x && rect.x + rect.width >= mouse.x && rect.y <= mouse.y && rect.y + rect.height >= mouse.y)
58 return true;
59 return false;
60 }
61
62 private Vector2 convertTo800x480(Vector3 position)
63 {
64 float x = position.x * 800 / Screen.width;
65 float y = position.y * 480 / Screen.height;
66 return new Vector2(x, y);
67 }
68 }
2 using System.Collections;
3
4 public class InputProcessor : MonoBehaviour {
5 private Rect rect;
6 private Vector2 position;
7 private bool isTouchedDown;
8 private float width;
9 private float height;
10
11 public void Start()
12 {
13 position = new Vector2((transform.position.x + 4) * 100, (transform.position.y + 2.4f) * 100);
14 width = gameObject.GetComponent<SpriteRenderer>().sprite.rect.width;
15 height = gameObject.GetComponent<SpriteRenderer>().sprite.rect.height;
16 rect = new Rect(position.x - width / 2, position.y - height / 2, width, height);
17 }
18
19 private void calculateTransform()
20 {
21 position = new Vector2((transform.position.x + 4) * 100, (transform.position.y + 2.4f) * 100);
22 //width = gameObject.GetComponent<SpriteRenderer>().sprite.rect.width;
23 //height = gameObject.GetComponent<SpriteRenderer>().sprite.rect.height;
24 rect = new Rect(position.x - width / 2, position.y - height / 2, width, height);
25 }
26
27 public void Update()
28 {
29 if (!InputController.Enabled) return;
30 calculateTransform();
31 if (Input.GetButtonDown("Fire1"))
32 {
33 if (checkBounds(rect, convertTo800x480(Input.mousePosition)))
34 {
35 isTouchedDown = true;
36 if(gameObject.GetComponent<InputAdapter>() != null)
37 gameObject.GetComponent<InputAdapter>().OnTouchDown();
38 InputController.IsScreen = false;
39 }
40 }
41 else if (Input.GetButtonUp("Fire1"))
42 {
43 if (gameObject.GetComponent<InputAdapter>() != null)
44 gameObject.GetComponent<InputAdapter>().OnCheckUp();
45 Vector2 mouse = convertTo800x480(Input.mousePosition);
46 if (isTouchedDown && checkBounds(rect, convertTo800x480(Input.mousePosition)))
47 {
48 if(gameObject.GetComponent<InputAdapter>() != null)
49 gameObject.GetComponent<InputAdapter>().OnTouchUp();
50 }
51 isTouchedDown = false;
52 }
53 }
54
55 private bool checkBounds(Rect rect, Vector2 mouse)
56 {
57 if (rect.x <= mouse.x && rect.x + rect.width >= mouse.x && rect.y <= mouse.y && rect.y + rect.height >= mouse.y)
58 return true;
59 return false;
60 }
61
62 private Vector2 convertTo800x480(Vector3 position)
63 {
64 float x = position.x * 800 / Screen.width;
65 float y = position.y * 480 / Screen.height;
66 return new Vector2(x, y);
67 }
68 }
width = gameObject.GetComponent
height = gameObject.GetComponent